<template>
	<view class="back-view">
		<view class="view-background"></view>
		<view class="white-view-back">
			<view class="top-back">
				<view class="top-name">批量新增权益</view>
				<view class="close-back" @click="closeBtnAction">
					<image class="close-img" src="../../../static/close.png"></image>
				</view>
			</view>

			<view class="top-line"></view>
			<view class="download-back">
				<view class="download-des">下载导入模板，根据模板完善内容</view>
				<view class="download-btn" @click="downloadTemplateAction">下载模板</view>
			</view>
			<view class="file-back">
				<image class="file-image" src="../../../static/phone/case-detail-more.png"></image>
				<view class="file-add" @click="selectFile">上传文件</view>
				<view class="file-des">仅支持xls、xlsx格式文件</view>
				<scroll-view class="file-list" scroll-y="true">
					<view class="file-list-item" v-if="fileItem.name">
						<view class="file-list-name">{{fileItem.name}}</view>
						<image class="file-list-image" @click="deleteFile" src="../../../static/phone/icon_detele.png">
						</image>
					</view>
				</scroll-view>
			</view>
			<view class="bottom-back">
				<view class="bottom-btn bottom-btn-cancel" @click="closeBtnAction">取消</view>
				<view class="bottom-btn" @click="uploadFile">确定</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				fileItem: {
					name: '',
					path: ''
				},
			}
		},
		mounted() {

		},
		methods: {
			//下载模板
			downloadTemplateAction() {
				var wxflie = uni.getFileSystemManager();
				var _this = this;
				uni.downloadFile({
					url: getApp().globalData.baseUrl + "/admin/customerRights/template",
					header: {
						"Content-Type": "application/x-xls",
						"Authorization": "Basic ZG90Z286ZG90Z28=",
						"token": uni.getStorageSync("accessToken") ? uni.getStorageSync("accessToken") : ''
					},
					success: function(res) {
						wx.hideLoading();
						console.log(res)
						wxflie.saveFile({
							tempFilePath: res.tempFilePath,
							filePath: `${wx.env.USER_DATA_PATH}/服务权益模板.xls`,
							success: function(res1) {
								console.log(res1)
								wx.saveImageToPhotosAlbum({
										 filePath: res1.savedFilePath,
									}).then(save => {
										console.log('保存成功', save)
										wx.showToast({
											title: '保存成功',
											icon: 'success',
											duration: 2000
										})
										
									})
									.catch(error => {
										console.log('保存失败', error)
									})
							}
						})
					},
					fail: function(err) {
						wx.hideLoading();
						console.log('下载失败：', err);
					},
				});
				
				// var wxflie = uni.getFileSystemManager();
				// var _this = this;
				// uni.downloadFile({
				// 	url: getApp().globalData.baseUrl + "/admin/customerRights/template",
				// 	header: {
				// 		"Content-Type": "application/x-xls",
				// 		"Authorization": "Basic ZG90Z286ZG90Z28=",
				// 		"token": uni.getStorageSync("accessToken") ? uni.getStorageSync("accessToken") : ''
				// 	},
				// 	success: function(res) {
				// 		wx.hideLoading();

				// 		wx.saveImageToPhotosAlbum({
				// 				tempFilePath: res.tempFilePath,
				// 				filePath: `${wx.env.USER_DATA_PATH}/客户管理模板.xls`,
				// 				fileType: 'xls'
				// 			}).then(save => {
				// 				console.log('保存成功', save)
				// 				wx.showToast({
				// 					title: '保存成功',
				// 					icon: 'success',
				// 					duration: 2000
				// 				})

				// 			})
				// 			.catch(error => {
				// 				console.log('保存失败', error)
				// 			})
				// 	},
				// 	fail: function(err) {
				// 		wx.hideLoading();
				// 		console.log('下载失败：', err);
				// 	},
				// });


				// var _this = this;
				// uni.downloadFile({
				// 	url: getApp().globalData.baseUrl + "/admin/customerRights/template",
				// 	header: {
				// 		"Content-Type": "application/x-xls",
				// 		"Authorization": "Basic ZG90Z286ZG90Z28=",
				// 		"token": uni.getStorageSync("accessToken") ? uni.getStorageSync("accessToken") : ''
				// 	},
				// 	success: function(res) {
				// 		console.log(res)
				// 		wx.hideLoading();
				// 		wx.saveImageToPhotosAlbum({
				// 				tempFilePath: res.tempFilePath,
				// 				filePath: `${wx.env.USER_DATA_PATH}/服务权益模板.xls`,
				// 				fileType: 'xls'
				// 			}).then(save => {
				// 				console.log('保存成功', save)
				// 				wx.showToast({
				// 					title: '保存成功',
				// 					icon: 'success',
				// 					duration: 2000
				// 				})
				// 			})
				// 			.catch(error => {
				// 				console.log('保存失败', error)
				// 			})
				// 	},
				// 	fail: function(err) {
				// 		wx.hideLoading();
				// 		console.log('下载失败：', err);
				// 	},
				// });
			},

			//上传文件
			uploadFile() {
				if (!this.fileItem.path) {
					uni.showToast({
						title: '请上传文件',
						icon: 'none'
					})
					return;
				}
				var _this = this;
				var url = getApp().globalData.baseUrl + "/admin/customerRights/import";

				uni.uploadFile({
					url: url,
					header: {
						"Content-Type": "multipart/form-data",
						"Authorization": "Basic ZG90Z286ZG90Z28=",
						"token": uni.getStorageSync("accessToken") ? uni.getStorageSync("accessToken") : ''
					},
					filePath: this.fileItem.path,
					name: 'file',
					success: (res) => {
						console.log(res.data);
						var respon = JSON.parse(res.data)
						if (respon.code == 200) {
							uni.showToast({
								title: '导入成功'
							})
							_this.$emit("successBack");
							_this.closeBtnAction()
						} else {
							uni.showToast({
								icon: 'none',
								title: respon.msg
							})
						}

					}
				})
			},


			// 获取上传状态
			selectFile() {
				console.log('222')
				var _this = this;
				uni.chooseMessageFile({
					count: 1,
					type: 'file',
					success: (res) => {
						console.log(res)
						let filename = res.tempFiles[0].name;
						if (filename.indexOf(".xls") == -1 && filename.indexOf(".xlsx") == -1) {
							uni.showModal({
								title: '提示',
								content: '文件类型必须为excel！(.xls或.xlsx)',
								confirmColor: '#0177ff',
								confirmText: '确定',
							})
						} else {
							_this.fileItem = res.tempFiles[0];
						}
					}
				})
			},
			//删除文件
			deleteFile() {
				this.fileItem = {
					name: '',
					path: ''
				}
			},

			closeBtnAction() {
				this.$emit('importServeClose')
			},
			moveHandle() {
				return true
			},
		},
	}
</script>

<style>
	.back-view {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 199;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.view-background {
		position: absolute;
		width: 100%;
		height: 100%;
		z-index: 100;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.white-view-back {
		width: 314px;
		padding: 24px;
		background-color: white;
		margin-top: -50px;
		border-radius: 4px;
		z-index: 101;
		position: absolute;
	}

	.top-back {
		display: flex;
		justify-content: space-between;
	}

	.top-name {
		font-size: 18px;
		font-weight: normal;
		color: #333333;
	}

	.close-back {
		cursor: pointer;
		width: 20px;
		height: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.close-img {
		width: 12px;
		height: 12px;
	}

	.top-line {
		height: 1px;
		background: #EEEEEE;
		width: 100%;
		margin-top: 20px;
		margin-bottom: 20px;
	}

	.bottom-back {
		bottom: 0px;
		width: 100%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		position: absolute;
		height: 60px;
		right: 16px;
	}

	.bottom-btn {
		cursor: pointer;
		width: 104px;
		height: 32px;
		background: #2DA9C5;
		border-radius: 2px;
		color: #FFFFFF;
		font-size: 14px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.bottom-btn-cancel {
		margin-right: 10px;
		background: #FFFFFF;
		border: 1px solid #EEEEEE;
		color: #333333;
	}

	.download-back {
		width: 100%;
		background: #F7F7F7;
		border-radius: 4px;
		padding-top: 10px;
		padding-bottom: 10px;
	}

	.download-des {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 16px;
	}

	.download-btn {
		width: 104px;
		height: 32px;
		background: #FFFFFF;
		border-radius: 2px;
		border: 1px solid #EEEEEE;
		font-size: 14px;
		font-weight: normal;
		color: #333333;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 8px;
		margin-left: 16px;
	}

	.file-back {
		width: 100%;

		background: #F7F7F7;
		border-radius: 4px;
		margin-top: 8px;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 40px;
	}

	.file-image {
		width: 48px;
		height: 52px;
		margin-top: 16px;
	}

	.file-add {
		width: 104px;
		height: 32px;
		background: #FFFFFF;
		border-radius: 2px;
		border: 1px solid #EEEEEE;
		font-size: 14px;
		font-weight: normal;
		color: #333333;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 10px;
	}

	.file-des {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-top: 8px;
	}

	.file-list {
		max-height: 100px;
		margin-bottom: 10px;
	}

	.file-list-item {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 5px;
		margin-top: 5px;
	}

	.file-list-name {
		font-size: 12px;
		font-weight: 400;
		color: #2DA9C5;
		max-width: 80%;
		word-wrap: break-word;
		word-break: break-all;
		white-space: pre-line;
	}

	.file-list-image {
		margin-left: 8px;
		width: 14px;
		height: 14px;
	}
</style>
